<?php

/**
 * 这是一个表 "children" 的模型类。
 *
 * 表 'children' 有以下字段:
 * @property integer $id
 * @property integer $party_id
 * @property string $username
 * @property string $sex
 * @property integer $age
 * @property string $grade
 * @property string $school
 *
 * 以下是表的关联关系:
 * @property Party $party
 */
class Children extends CActiveRecord
{
	/**
	 * @return string 表名
	 */
	public function tableName()
	{
		return 'children';
	}

	/**
	 * @return array 表字段验证规则的数组。
	 */
	public function rules()
	{
		return array(
                        array('party_id,username,sex,age', 'required'),
			array('party_id, age', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>5),
			array('sex', 'length', 'max'=>1),
			array('grade, school', 'length', 'max'=>255),
			// 以下规则用于验证查询函数，如果不做查询可注释此行。
			array('id, party_id, username, sex, age, grade, school', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array 关联关系。
	 */
	public function relations()
	{
		return array(
			'party' => array(self::BELONGS_TO, 'Party', 'party_id'),
		);
	}

	/**
	 * @return array 自定义字段显示文本标签 (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '序号',
			'party_id' => '家长',
			'username' => '儿童姓名',
			'sex' => '性别',
			'age' => '年龄',
			'grade' => '年级',
			'school' => '学校',
		);
	}

	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('party_id',$this->party_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('age',$this->age);
		$criteria->compare('grade',$this->grade,true);
		$criteria->compare('school',$this->school,true);
		$criteria->order='id desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_clander($party_ids)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
//		$criteria->compare('party_id',$this->party_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('age',$this->age);
		$criteria->compare('grade',$this->grade,true);
		$criteria->compare('school',$this->school,true);
		$criteria->order='id desc';//排序字段
                $criteria->addInCondition('party_id', $party_ids);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_age($age,$party_ids,$type)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
//		$criteria->compare('party_id',$this->party_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('sex',$this->sex,true);
//		$criteria->compare('age',$this->age);
		$criteria->compare('grade',$this->grade,true);
		$criteria->compare('school',$this->school,true);
		$criteria->order='id desc';//排序字段
                switch ($age) {
                    case 1:
                        $criteria->addCondition('age<=3');
                        break;
                    case 2:
                        $criteria->addCondition('age>=3 and age<=6');
                        break;
                    case 3:
                        $criteria->addCondition('age>=6 and age<=12');
                        break;       
                    case 4:
                        $criteria->addCondition('age>=12 and age<=16');
                        break;     
                    case 5:
                        $criteria->addCondition('age>=16 and age<=20');
                        break;                       
                }
                if($type==1){
                    $criteria->addInCondition('party_id', $party_ids);
                }
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}         
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_msg($party_id)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('party_id',$party_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('age',$this->age);
		$criteria->compare('grade',$this->grade,true);
		$criteria->compare('school',$this->school,true);
		$criteria->order='id desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}        

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Children the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
